<?php
$filename = './token.txt';//key文件
$token = file_get_contents($filename);
$headerArray = [
   "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36",
   "Rererer: https://www.aliyundrive.com/",
   "Content-Type: application/json"
];
echo '当前token: '.$token.PHP_EOL;
$api = 'https://api.aliyundrive.com/token/refresh';
$data = curlPost($api, ['refresh_token'=>$token], $headerArray, 'json');
$ntoken = json_decode($result,true)['refresh_token']??"";
if(!$ntoken){
   $api2 = 'https://auth.aliyundrive.com/v2/account/token';
   $data2 = curlPost($api2, ['refresh_token'=>$token,'grant_type'=>'refresh_token'], $headerArray, 'json');
   $ntoken = json_decode($data2,true)['refresh_token']??"";
}
if($ntoken){
   echo '最新token: '.$ntoken.PHP_EOL;
   file_put_contents($filename, $ntoken);
   die("更新完成");
}
function curlPost($url, $post_data = array(), $header = "", $data_type = "") {
    $header = empty($header) ? [] : $header;
    //支持json数据数据提交
    if($data_type == 'json'){
        $post_string = json_encode($post_data);
    }elseif($data_type == 'array') {
        $post_string = $post_data;
    }elseif(is_array($post_data)){
        $post_string = http_build_query($post_data, '', '&');
    }

    $ch = curl_init();    // 启动一个CURL会话
    curl_setopt($ch, CURLOPT_URL, $url);     // 要访问的地址
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  // 对认证证书来源的检查   // https请求 不验证证书和hosts
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器

    curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的Post请求
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);     // Post提交的数据包
    curl_setopt($ch, CURLOPT_TIMEOUT, 3);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     // 获取的信息以文件流的形式返回
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模拟的header头
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

?>